package pixelradar.map;

import com.pixelmonmod.pixelmon.api.pokemon.Pokemon;
import com.pixelmonmod.pixelmon.client.gui.GuiResources;
import com.pixelmonmod.pixelmon.entities.pixelmon.EntityPixelmon;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import pixelradar.entities.EntityType;
import pixelradar.map.mapmode.MapMode;
import pixelradar.util.Render;

import java.awt.geom.Point2D.Double;

public class PKMarker {
    private static final double BORDER_WIDTH = 0.4D;
    private static final int BORDER_COLOR = -16777216;
    public Double screenPos = new Double(0.0D, 0.0D);
    public String name;
    public String desc;
    public double r;
    public double x;
    public double y;
    public double z;
    public int coordX;
    public int coordY;
    public int coordZ;
    public int dimension;
    public EntityType type;
    public float yaw;
    ResourceLocation sprite;

    public PKMarker(EntityType type, Entity entity, String name, String desc, double radius, MapRenderer mp, int dimension) {
        this.name = name;
        if (entity instanceof EntityPixelmon) {
            EntityPixelmon entityPixelmon = (EntityPixelmon) entity;
            Pokemon pokemon = entityPixelmon.getPokemonData();
            if (pokemon.getForm() > 0 && entityPixelmon.getBaseStats() != null) {
                this.name = pokemon.getFormEnum().getLocalizedName() + " " + this.name;
            }

            if (pokemon.isShiny()) {
                this.name = this.name + " (" + I18n.format("mw.finder.msg.shiny") + ")";
            }

            this.sprite = GuiResources.getPokemonSprite(pokemon);
        }

        this.yaw = entity.rotationYaw;
        this.dimension = Minecraft.getMinecraft().world.provider.getDimension();
        this.type = type;
        this.r = radius;
        this.desc = desc;
        this.x = entity.posX;
        this.y = entity.posY;
        this.z = entity.posZ;
        this.coordX = (int) this.x;
        this.coordY = (int) this.y;
        this.coordZ = (int) this.z;
        double scale = mp.getMapView().getDimensionScaling(Minecraft.getMinecraft().world.provider.getDimension());
        Double p = mp.getMapMode().getClampedScreenXY(mp.getMapView(), this.x * scale, this.z * scale);
        this.screenPos.setLocation(p.x + (double) mp.getMapMode().getXTranslation(), p.y + (double) mp.getMapMode().getYTranslation());
    }

    public PKMarker(EntityType type, TileEntity entity, String name, String desc, double radius, MapRenderer mp, int dimension) {
        this.yaw = 0.0F;
        this.dimension = Minecraft.getMinecraft().world.provider.getDimension();
        this.type = type;
        this.r = radius;
        this.name = name;
        this.desc = desc;
        this.x = (double) entity.getPos().getX();
        this.y = (double) entity.getPos().getY();
        this.z = (double) entity.getPos().getZ();
        this.coordX = (int) this.x;
        this.coordY = (int) this.y;
        this.coordZ = (int) this.z;
        double scale = mp.getMapView().getDimensionScaling(Minecraft.getMinecraft().world.provider.getDimension());
        Double p = mp.getMapMode().getClampedScreenXY(mp.getMapView(), this.x * scale, this.z * scale);
        this.screenPos.setLocation(p.x + (double) mp.getMapMode().getXTranslation(), p.y + (double) mp.getMapMode().getYTranslation());
    }

    private boolean judgeTile(MapMode mapMode, MapView mapView) {
        if (this.type != EntityType.LOOT && this.type != EntityType.SHRINE && this.type != EntityType.GADGET && this.type != EntityType.DEN && this.type != EntityType.WORMHOLE) {
            return false;
        } else {
            this.drawTile(mapMode, mapView);
            return true;
        }
    }

    public void draw(MapMode mapMode, MapView mapView) {
        if (!this.judgeTile(mapMode, mapView)) {
            double scale = mapView.getDimensionScaling(mapView.getDimension());
            Double p = mapMode.getClampedScreenXY(mapView, this.x * scale, this.z * scale);
            Render.setColour(this.type.getColor().getRGB());
            Render.drawCircle(p.x, p.y, this.type.getRadius());
            Render.setColour(-16777216);
            Render.drawCircleBorder(p.x, p.y, this.type.getRadius(), 0.4D);
        }
    }

    public void drawPKTexture(MapMode mapMode, MapView mapView, float mapRotationDegrees) {
        if (!this.judgeTile(mapMode, mapView)) {
            double scale = mapView.getDimensionScaling(mapView.getDimension());
            Double p = mapMode.getClampedScreenXY(mapView, this.x * scale, this.z * scale);
            double markerSize = this.type.getTextureDimension();
            Render.setColour(-1);
            if (this.sprite != null) {
                Minecraft.getMinecraft().renderEngine.bindTexture(this.sprite);
            }

            Render.drawTexturedRect(p.x - markerSize / 2.0D, p.y - markerSize / 2.0D, markerSize, markerSize);
        }
    }

    public void drawTile(MapMode mapMode, MapView mapView) {
        double scale = mapView.getDimensionScaling(mapView.getDimension());
        Double p = mapMode.getClampedScreenXY(mapView, this.x * scale, this.z * scale);
        Render.setColour(this.type.getColor().getRGB());
        Render.drawRect(p.x, p.y, this.type.getRadius(), this.type.getRadius());
        Render.setColour(-16777216);
        Render.drawRectBorder(p.x, p.y, this.type.getRadius(), this.type.getRadius(), 0.8D);
    }

    public double getDistanceToMarker(Entity entityIn) {
        if (entityIn == null) {
            return 0.0D;
        } else {
            double d0 = this.x - entityIn.posX;
            double d1 = this.y - entityIn.posY;
            double d2 = this.z - entityIn.posZ;
            return (double) MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
        }
    }
}
